package cn.rongdao.jrkabao.ui.financing.uicomponent;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.CashAdvanceBean;

/**
 * Created by jack on 2017/9/8
 */

public class ItemFinancingUIComponent extends LinearLayout {
    public ItemFinancingUIComponent(Context context) {
        super(context);
    }

    public ItemFinancingUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemFinancingUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private ImageView ivAvatar;
    private TextView tvName;
    private TextView tvContent;
    private TextView tvAnnualized;
    private TextView tvTerm;
    private TextView tvDescription;
    private ImageView ivDesImage;
    private View viewLine;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ivAvatar = (ImageView) findViewById(R.id.if_iv_avatar);
        tvName = (TextView) findViewById(R.id.if_tv_name);
        tvContent = (TextView) findViewById(R.id.if_tv_content);
        tvAnnualized = (TextView) findViewById(R.id.if_tv_annualized);
        tvTerm = (TextView) findViewById(R.id.if_tv_term);
        tvDescription = (TextView) findViewById(R.id.if_tv_description);
        ivDesImage = (ImageView) findViewById(R.id.if_iv_des_image);
        viewLine = findViewById(R.id.if_view_line);
    }

    public void setContent(CashAdvanceBean.FinancialListBean financialListBean) {
        setVisibility(View.VISIBLE);
        if (financialListBean.isLast()) {
            viewLine.setVisibility(View.GONE);
        }
        FRImageLoad.loadImage(FRString.valueOf(financialListBean.getOrg_pic()), ivAvatar);
        tvName.setText(FRString.valueOf(financialListBean.getOrg_name()));
        tvContent.setText(FRString.valueOf(financialListBean.getOrg_des()));
        tvAnnualized.setText(getRate(financialListBean.getRate(), financialListBean.getRate_text()));
        tvTerm.setText(getTerm(financialListBean.getDeadline(), financialListBean.getDeadline_text()));
        tvDescription.setText(FRString.valueOf(financialListBean.getBottom_text()));
        FRImageLoad.loadImage(FRString.valueOf(financialListBean.getBottom_icon()), ivDesImage);
    }

    private SpannableString getRate(String rate, String rateText) {
        String annualized = FRCollection.appendAsString(rate, "%\n", rateText, "");
        int index = annualized.indexOf("%");
        SpannableString ss = new SpannableString(annualized);
        ss.setSpan(new AbsoluteSizeSpan(16, true), index, index + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(12, true), index + 1, annualized.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), index + 1, annualized.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    }

    private SpannableString getTerm(String deadline, String deadlineText) {
        String term = FRCollection.appendAsString(deadline, deadlineText, "\n");
        SpannableString ss = new SpannableString(term);
        ss.setSpan(new AbsoluteSizeSpan(12, true), deadline.length(), term.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.parseColor("#999999")), deadline.length(), term.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    }
}